﻿/* Created by: Subodh Shrestha
 * Contact: forsubodh@gmail.com */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TravelExpertsManagementConsole
{
    public partial class formSupplier : Form
    {
        public enum methodcalltype
        {
            add = 0,
            update = 1,
        }

        public formSupplier(frmContainer parent)
        {
            InitializeComponent();

            MdiParent = parent;
            setAccessControls(parent.accessLevel);
        }

        public void setAccessControls(int accessLevel){
            switch(accessLevel){
                case 3:
                    btnNew.Enabled = false;
                    btnClear.Enabled = false;
                    btnSubmit.Enabled = false;
                    editSupplierContacts.Enabled = false;
                    break;
                default:
                    break;
            }
        }

        private void formSupplier_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmContainer parent = (frmContainer)MdiParent;

            parent.childSupplier = null;
        }

        private void editSupplierContacts_Click(object sender, EventArgs e)
        {
            frmContainer parent = (frmContainer)MdiParent;
            if (parent.childSupplierContact == null)
            {
                parent.childSupplierContact = new formSupplierContact((frmContainer)MdiParent, ((Supplier)listBoxSupplier.SelectedItem).SupplierId);
                parent.childSupplierContact.Show();
            }
            parent.childSupplierContact.Focus();
        }

        private void formSupplier_Load(object sender, EventArgs e)
        {
            refreshList();
        }

        public void refreshList()
        {
            try
            {
                DataAccess<Supplier> myDataAccess = new DataAccess<Supplier>();
                List<Supplier> productList = myDataAccess.select();
                listBoxSupplier.DataSource = productList;

                tbSuppId.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void listBoxSupplier_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxSupplier.SelectedIndex == -1)
            {
                return;
            }
            Supplier selectedSupplier = (Supplier)listBoxSupplier.SelectedItem;
            tbSuppId.Text = selectedSupplier.SupplierId.ToString();
            tbSuppName.Text = selectedSupplier.SupName;
            listBoxSuppProducts.DataSource = selectedSupplier.ProductList;
            tbSuppId.Enabled = false;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            tbSuppId.Text = "";
            tbSuppName.Text = "";
            tbSuppId.Enabled = true;
            listBoxSupplier.SelectedIndex = -1;
            listBoxSuppProducts.DataSource = null;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if(!validateInput(methodcalltype.add))
            {
                return;
            }
            try
            {
                DataAccess<Supplier> myDataAccess = new DataAccess<Supplier>();
                Supplier newSupplier = new Supplier();
                newSupplier.SupName = tbSuppName.Text;
                newSupplier.SupplierId = int.Parse(tbSuppId.Text);
                newSupplier.ProductList = new List<Product>();
                foreach (Product prod in listBoxSuppProducts.Items)
                {
                    newSupplier.ProductList.Add(prod);
                }
                myDataAccess.Add(newSupplier);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            refreshList();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (listBoxSupplier.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a supplier");
                return;
            }
            try
            {
                DataAccess<Supplier> myDataAccess = new DataAccess<Supplier>();
                Supplier selectedSupplier = (Supplier)listBoxSupplier.SelectedItem;
                myDataAccess.Delete(selectedSupplier);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            refreshList();
        }

        private bool validateInput(methodcalltype func)
        {
            string errMsg = "";
            if (tbSuppId.Enabled == true)
            {
                if (tbSuppId.Text == "")
                {
                    errMsg += "Please enter an Id" + Environment.NewLine;
                }
                else
                {
                    int id;
                    if (!int.TryParse(tbSuppId.Text, out id))
                    {
                        errMsg += "Supplier Id must be an integer" + Environment.NewLine;
                    }
                }
            }
            else if(tbSuppId.Enabled == false && func == methodcalltype.add)
            {
                errMsg += "Cannot create duplicate Supplier" + Environment.NewLine;
            }
            if (tbSuppName.Text == "")
            {
                errMsg += "Please enter Supplier Name" + Environment.NewLine;
            }
            else
            {
                Double number;
                if (Double.TryParse(tbSuppName.Text, out number))
                {
                    errMsg += "Supplier Name must be string" + Environment.NewLine;
                }
            }
            if (errMsg == "")
            {
                return true;
            }
            else
            {
                MessageBox.Show(errMsg);
                return false;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (listBoxSupplier.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a supplier");
                return;
            }
            if (!validateInput(methodcalltype.update))
            {
                return;
            }
            try
            {
                DataAccess<Supplier> myDataAccess = new DataAccess<Supplier>();
                Supplier updateSupplier = new Supplier();
                updateSupplier.SupplierId = int.Parse(tbSuppId.Text);
                updateSupplier.SupName = tbSuppName.Text;
                updateSupplier.ProductList = new List<Product>();
                foreach (Product prod in listBoxSuppProducts.Items)
                {
                    updateSupplier.ProductList.Add(prod);
                }
                myDataAccess.Update(updateSupplier);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            refreshList();
        }

        private void formSupplier_Enter(object sender, EventArgs e)
        {
            listBoxAllProducts.DataSource = DataAccess<Supplier>.setAllProducts();
            refreshList();
        }

        private void addToSuppProducts_Click(object sender, EventArgs e)
        {
            if (listBoxAllProducts.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a Product");
                return;
            }

            Product selectedProduct = (Product)listBoxAllProducts.SelectedItem;
            List<Product> productList;
            if ((List<Product>)listBoxSuppProducts.DataSource != null)
            {
                productList = (List<Product>)listBoxSuppProducts.DataSource;
            }
            else
            {
                productList = new List<Product>();
                listBoxSuppProducts.DataSource = productList;
            }
            //check if the supplier-to-add already exists in the product's supplier list
            Product findMatch = productList.Find(delegate(Product p) { return p.ProductID == selectedProduct.ProductID; });
            if (findMatch == null)
            {
                productList.Add(selectedProduct);
            }
            ((CurrencyManager)listBoxSuppProducts.BindingContext[listBoxSuppProducts.DataSource]).Refresh();
        }

        private void removeFromSuppProducts_Click(object sender, EventArgs e)
        {
            if (listBoxSuppProducts.DataSource == null)
            {
                return;
            }
            if (listBoxSuppProducts.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a Product");
                return;
            }

            Product selectedProduct = (Product)listBoxSuppProducts.SelectedItem;
            List<Product> productList = (List<Product>)listBoxSuppProducts.DataSource;
            productList.Remove(selectedProduct);
            ((CurrencyManager)listBoxSuppProducts.BindingContext[listBoxSuppProducts.DataSource]).Refresh();
        }
    }
}
